import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:get/get.dart';

class InvitationPosterPage extends StatelessWidget {

  final CommonPageController commonPageController = Get.find();
  final GlobalKey globalKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: '海报邀请',
        backgroundColor: pageBgColor,
        actions: [
          Container(
            margin: EdgeInsets.only(right: 12.w),
            child: InkWellUtil.shakeProofInkWell(Image.asset(
              'assets/icons/icon_share.png',
              width: 20.w,
              height: 20.w,
            ), () {
              showModalBottomSheet(context: context, builder: (context) {
                return Container(
                  height: 163.h + ScreenUtil().bottomBarHeight,
                  width: 375.w,
                  padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight),
                  color: bgWhiteColor,
                  child: Column(
                    children: [
                      Expanded(child: Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.symmetric(horizontal: 39.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            _shareItem(title: '微信', path: 'assets/icons/icon_weChar_share.png', shareFun: () async {
                              commonPageController.shareToWeChar();
                            }),
                            _shareItem(title: 'QQ', path: 'assets/icons/icon_qq_share.png', shareFun: () {
                              commonPageController.shareToQQ();
                            }),
                            _shareItem(title: '保存图片', path: 'assets/icons/icon_save_image.png', shareFun: () {
                              commonPageController.saveImage(globalKey);
                            })
                          ],
                        ),
                      )),
                      InkWellUtil.shakeProofInkWell(Container(
                        height: 54.h,
                        width: 375.w,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          border: Border(
                            top: BorderSide(
                              color: lineColor,
                              width: .5
                            )
                          )
                        ),
                        child: Text(
                          '取消',
                          style: TextStyle(
                            color: textBlackColor,
                            fontSize: 16.sp
                          ),
                        ),
                      ), () {
                        Get.back();
                      })
                    ],
                  ),
                );
              });
            }),
          )
        ],
      ),
      body: RepaintBoundary(
        key: globalKey,
        child: _content(),
      )
    );
  }

  Widget _content() {
    return Stack(
      children: [
        Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(
                'assets/images/invitation_bg.png',
              ),
              fit: BoxFit.fill
            )
          ),
        ),
        Positioned(
          bottom: 44.h,
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 8.w),
            width: 375.w,
            child: Container(
              padding: EdgeInsets.all(12.w),
              decoration: BoxDecoration(
                color: bgInvitation1,
                borderRadius: BorderRadius.circular(12.w)
              ),
              child: Container(
                height: 157.h,
                decoration: BoxDecoration(
                  color: bgInvitation2,
                  borderRadius: BorderRadius.circular(12.w)
                ),
                child: Row(
                  children: [
                    Expanded(child: Container(
                      padding: EdgeInsets.symmetric(vertical: 21.h),
                      child: Container(
                        decoration: BoxDecoration(
                          border: Border(
                            right: BorderSide(
                              color: textBlackColor,
                              width: .5
                            )
                          )
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text(
                              '我的邀请码',
                              style: TextStyle(
                                color: textBlackColor,
                                fontSize: 16.sp,
                                fontWeight: FontWeight.bold
                              ),
                            ),
                            SizedBox(height: 19.h),
                            Text(
                              '${commonPageController.userData.value.invitationCode}',
                              style: TextStyle(
                                color: mainTextColor,
                                fontWeight: FontWeight.bold,
                                fontSize: 24.sp
                              ),
                            )
                          ],
                        ),
                      ),
                    )),
                    Expanded(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          QrImage(
                            backgroundColor: bgWhiteColor,
                            data: '${commonPageController.userData.value.invitationUrl}',
                            version: QrVersions.auto,
                            size: 90.w,
                            padding: EdgeInsets.all(6.w),
                          ),
                          SizedBox(height: 5.h),
                          Text(
                            '长按二维码直接进入',
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 12.sp
                            ),
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
          )
        )
      ],
    );
  }

  /// 分享项
  Widget _shareItem({ required String title, required String path, required Function shareFun }) {
    return InkWellUtil.shakeProofInkWell(Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            path,
            width: 48.w,
            height: 48.w,
          ),
          SizedBox(height: 8.h),
          Text(
            title,
            style: TextStyle(
              color: textBlackColor,
              fontSize: 14.sp
            ),
          )
        ],
      ),
    ), () => shareFun());
  }
}